﻿using NewRen.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Schemas
{
    public class AccessShowBusinessAttribute : BaseAuthAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            using (var db = new DB())
            {
                var id = 0;
                if (((MvcHandler)httpContext.Handler).RequestContext.RouteData.Values["id"] != null)
                    id = Convert.ToInt32(((MvcHandler)httpContext.Handler).RequestContext.RouteData.Values["id"]);
                else
                    id = Convert.ToInt32(httpContext.Request.Form["id"]);
                var business = db.Businesses.Find(id);

                if (!httpContext.User.Identity.Name.Equals(business.LoginName))
                {
                    return false;
                }

            }
            return true;
        }
    }
}
